<template>
	<view>
		<u-toast ref="uToast"></u-toast>
		<view class="group">
			<view class="title flex1">当前设备</view>
			<view>{{connectInfo==null ? '没有连接' : connectInfo.name}}</view>
		</view>
		<button @tap="printTest()" v-if="connectInfo!=null">打印测试</button>
		<view class="group">
			<view class="title flex1">蓝牙信息</view>
			<view>{{LYData.TS}}</view>
		</view>
		<view class="group" @tap="openP">
			<view class="title flex1">附近设备</view>
			<view :class="{loading:LYData.BLSO}"><u-icon name="reload" size="38"></u-icon></view>
		</view>
		<view class="list">
			<view v-for="(item,index) in LYSB" :key="index" class="item">
				<view class="name">{{item.name}}</view>
				<view class="strength">
					<view v-for="(key) in 5" :class="'strength'+key + ' ' + (item.strength>key ? 'checked' : '')">
					</view>
				</view>
				<view class="btn" v-if="item.status==0" @tap="connect(item)">连 接</view>
				<view class="btn" v-else-if="item.status==1">连接中</view>
				<view class="btn" v-else-if="item.status==2" @tap="disconnect(item)">断 开</view>
			</view>
		</view>
		<button style="margin-top: 40rpx;" @tap="$bluetooth.reset">重置蓝牙连接</button>
	</view>
</template>

<script>
	const util = require('./../utils/util.js')
	var pos = require('./../utils/pos.js')
	var GXZT; //更新状态
	var that;
	export default {
		data() {
			return {
				show: true,
				connectInfo: {

				},
				LYData: {
					openOK: false, //蓝牙是否打开
					BLZT: false, //蓝牙状态是否可用
					BLSO: false, //蓝牙是否正在搜索
					TS: "",
				},
				LYSB: [],
				receiveData: "",
				is_Print_text_Disabled: true,
				is_Print_barcode_Disabled: true,
				is_Print_order_Disabled: true,
				is_Print_curve_Disabled: true,
				is_Print_Self_Disabled: true,
				is_search_Disabled: false,
				interval: 1
			}
		},
		onShow() {
			that = this;
			that.interval = setInterval(() => {
				that.LYData = this.$bluetooth.GETData()
				that.connectInfo = this.$bluetooth.GetconnectInfo()
				that.LYSB = this.$bluetooth.GETSB()
			}, 500)
		},
		onUnload() {
			clearInterval(that.interval)
		},
		onHide() {
			clearInterval(that.interval)
		},
		methods: {
			showToast(params) {
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.navigateTo({
							url: params.url
						})
					}
				})
			},
			openP: function(e) {
				this.$bluetooth.openBlueTooth(function() {
					that.$bluetooth.checkBluetoothAdapterState(that.search)
				}, function(res) {
					if (res.errno == 0) {
						that.showToast({
							type: 'error',
							icon: false,
							title: res.errMsg,
						})
					} else if (res.errno == 103) {
						console.log(res)
						uni.showModal({
							title: '请授权',
							content: '请通过蓝牙授权',
							success(res) {
								if (res.confirm) {
									uni.openSetting()
								}
							}
						})
					}else if (res.errno == 1500102) {
						console.log(res)
						uni.showModal({
							title: '请先打开蓝牙',
							content: '请先打开蓝牙开关',
							success(res) {
								if (res.confirm) {
									uni.openSetting()
								}
							}
						})
					}else{
						uni.showToast({
							title:'蓝牙打开失败'
						})
						console.log(res)
					}
				});
			},
			search: function() {
				console.log('调用开始')
				this.$bluetooth.searchDevices(this.onfound, {
					duration: 10000
				})
			},
			onfound: function(res) {},
			connect: function(item) {
				that.$bluetooth.connect(item, this.onConnect)
			},
			onConnect: function(res) {
				if (res == true) {
					that.$bluetooth.getServices(function(res) {
						console.log('---------连接成功,启动接收监听------------',res);
						console.log(that.$bluetooth.dayinji())
						pos.startnotify(that);      // 监听接收事件
					})
				}
				uni.showToast({
					title:res
				})
				
			},
			disconnect: function(item) {
				that.$bluetooth.disconnect(item, this.onConnect)
			},
			printTest: function(e) {
				pos.AddPrintQueue('InitPos'); // 初始化蓝牙打印机 [27,64]
				pos.PrintTitle(" 打印机连接成功！"); 
				pos.PrintJumpLines(1);
				pos.DaYin(function(res) {
					if (res == "err") {
						pos.ClearQueue(); //清空打印列队
						//提示,打印出错,请稍后再试,仍然出错请重新设置打印机
					}
				});
			}
		}
	}
</script>

<style>
	.btn {
		width: 100rpx;
		background-color: rgb(252, 143, 65);
		color: #fff;
		text-align: center;
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 10rpx;
	}

	.list {
		padding: 0 30rpx;
	}

	.item {
		background-color: ;
		height: 90rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		background-color: #f1f1f1;
		padding: 0 20rpx;
		border-radius: 10rpx;
	}

	.item+.item {
		margin-top: 20rpx;
	}

	.item .name {
		width: 450rpx;
	}

	.item .strength {
		display: flex;
		align-items: flex-end;
		height: 40rpx;
	}

	.item .strength view {
		border-radius: 10rpx;
		background-color: #dedede;
		width: 6rpx;
		margin-left: 4rpx;
	}

	.item .strength .strength0 {
		height: 20%;
	}

	.item .strength .strength1 {
		height: 40%;
	}

	.item .strength .strength2 {
		height: 60%;
	}

	.item .strength .strength3 {
		height: 80%;
	}

	.item .strength .strength4 {
		height: 100%;
	}

	.item .strength .checked {
		background-color: rgb(252, 143, 65);
	}

	@keyframes rotate {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}

	.loading {
		animation-name: rotate;
		animation-duration: 1s;
		animation-iteration-count: infinite;
	}

	.group+.group {
		border-top: 1px solid #eee;
	}

	.group {
		display: flex;
		padding: 0 30rpx;
		align-items: center;
		height: 100rpx;
	}

	.group .title {
		font-size: 35rpx;
	}

	.flex1 {
		flex: 1;
	}
</style>